package web.api.base.system;

import blue.core.common.Page;
import blue.core.dict.DictParser;
import blue.core.dict.DictValue;
import blue.core.dict.State;
import blue.http.annotation.Http;
import blue.http.annotation.HttpMethod;
import blue.http.message.Request;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import web.base.system.facade.RoleFacade;
import web.base.system.message.RoleRequest;
import web.base.system.message.RoleResponse;

import java.util.List;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-02
 */
@Controller
@Http(url = "/base/system")
public class RoleController
{
	private RoleFacade roleFacade;
	private DictParser dictParser = DictParser.getInstance();

	@Autowired
	public RoleController(RoleFacade roleFacade)
	{
		this.roleFacade = roleFacade;
	}

	@Http(url = "/role_list", method = HttpMethod.POST)
	public JSONObject list(Request request)
	{
		RoleRequest param = request.getContentJson(RoleRequest.class);
		Page page = request.getContentJson(Page.class);
		page = roleFacade.list(param, page);
		List<DictValue> stateList = dictParser.getDictValue(State.class);

		JSONObject json = new JSONObject();
		json.put("page", page);
		json.put("state", stateList);
		return json;
	}

	@Http(url = "/role", method = HttpMethod.GET)
	public RoleResponse get(Request request)
	{
		Integer id = request.getQueryInt("id", 0);
		return roleFacade.getId(id);
	}

	@Http(url = "/role", method = HttpMethod.POST)
	public RoleResponse save(Request request)
	{
		RoleRequest roleRequest = request.getContentJson(RoleRequest.class);
		return roleFacade.save(roleRequest);
	}

	@Http(url = "/role", method = HttpMethod.DELETE)
	public void delete(Request request)
	{
		Integer id = request.getQueryInt("id", 0);
		roleFacade.deleteId(id);
	}

}
